<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>测试动态解析改进</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .header { background: #f0f8ff; padding: 20px; border-radius: 10px; margin-bottom: 20px; }
        .test-case { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .success { background: #d4edda; border-color: #28a745; }
        .failure { background: #f8d7da; border-color: #dc3545; }
        .warning { background: #fff3cd; border-color: #ffc107; }
        button { padding: 10px 20px; margin: 5px; border: none; border-radius: 5px; cursor: pointer; background: #007bff; color: white; }
    </style>
</head>
<body>
    <div class="header">
        <h1>🔄 动态解析改进测试</h1>
        <p>测试新的动态解析策略对混合VAL_TABLE_和BO_语句文件的处理效果</p>
    </div>

    <button onclick="runQuickTest()">快速测试重点文件</button>
    <button onclick="clearResults()">清除结果</button>

    <div id="results"></div>

    <script src="dbc-parser.js"></script>
    <script>
        const testFiles = [
            { name: 'cadillac_ct6_object.dbc', issue: '大量VAL_TABLE_混合帧定义' },
            { name: 'ford_lincoln_base_pt.dbc', issue: '大量VAL_TABLE_语句' },
            { name: 'ESR.dbc', issue: 'VAL_TABLE_和帧解析问题' },
            { name: 'tesla_model3_vehicle.dbc', issue: '多路复用信号' },
            { name: 'mazda_2017.dbc', issue: '数字开头标识符' },
            { name: 'acura_ilx_2016_nidec.dbc', issue: '基准测试' }
        ];

        function addResult(filename, status, details, issue) {
            const resultsDiv = document.getElementById('results');
            const testDiv = document.createElement('div');
            testDiv.className = `test-case ${status}`;
            
            let statusIcon = status === 'success' ? '✅' : status === 'failure' ? '❌' : '⚠️';
            testDiv.innerHTML = `
                <h3>${statusIcon} ${filename}</h3>
                <p><strong>测试场景:</strong> ${issue}</p>
                <p><strong>结果:</strong> ${details}</p>
            `;
            
            resultsDiv.appendChild(testDiv);
        }

        function clearResults() {
            document.getElementById('results').innerHTML = '';
        }

        async function testFile(filename, issue) {
            const dbcPath = `./${filename}`;
            
            try {
                const response = await fetch(dbcPath);
                if (!response.ok) throw new Error(`文件访问错误: ${response.status}`);
                
                const content = await response.text();
                const startTime = Date.now();
                const database = parseDBCContent(content, filename);
                const parseTime = Date.now() - startTime;
                
                const signals = database.frames.reduce((sum, frame) => sum + frame.signals.length, 0);
                let status = 'success';
                let details = `解析成功 - ${parseTime}ms, ${database.frames.length}帧, ${signals}信号, ${database.warnings.length}警告`;
                
                if (database.frames.length === 0) {
                    status = 'warning';
                    details = `解析成功但无帧 - ${parseTime}ms, ${database.warnings.length}警告`;
                }
                
                // 分析警告类型
                const valTableWarnings = database.warnings.filter(w => 
                    w.message.includes('VAL_TABLE_') || w.message.includes('跳过不支持的指令')
                ).length;
                
                if (valTableWarnings > 0) {
                    details += ` (VAL_TABLE_警告: ${valTableWarnings})`;
                }
                
                addResult(filename, status, details, issue);
                
                return { filename, status, frames: database.frames.length, signals, warnings: database.warnings.length };
            } catch (error) {
                addResult(filename, 'failure', `解析失败: ${error.message}`, issue);
                return { filename, status: 'failure', error: error.message };
            }
        }

        async function runQuickTest() {
            clearResults();
            
            console.log('🔄 开始动态解析改进测试...');
            
            let successCount = 0;
            let zeroFrameCount = 0;
            let totalFrames = 0;
            let totalSignals = 0;
            
            for (const file of testFiles) {
                const result = await testFile(file.name, file.issue);
                
                if (result.status === 'success') {
                    successCount++;
                    if (result.frames === 0) {
                        zeroFrameCount++;
                    } else {
                        totalFrames += result.frames;
                        totalSignals += result.signals;
                    }
                }
                
                // 短暂延迟
                await new Promise(resolve => setTimeout(resolve, 100));
            }
            
            // 显示总结
            setTimeout(() => {
                addResult('测试总结', 'success', 
                    `完成测试 ${testFiles.length} 个文件，成功 ${successCount} 个，0帧文件 ${zeroFrameCount} 个，总共解析 ${totalFrames} 帧 ${totalSignals} 信号`, 
                    '整体改进效果评估'
                );
            }, 500);
            
            console.log('✅ 动态解析改进测试完成');
        }

        document.addEventListener('DOMContentLoaded', function() {
            console.log('🔄 动态解析改进测试页面已加载');
        });
    </script>
</body>
</html> 